<?php

class JavascriptInputHelper
{
  /**
   * Returns the Javascript to disable or enable a given input or inputs when a checkbox's value changes.
   *
   * @param array $ids Inputs to disable when the condition is false.
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getEnableDisableJsForCheckbox($ids, $use_jquery = false)
  {
    return ($use_jquery === false) ? self::getEnableDisableJsFor($ids, 'on', '!=') : self::getEnableDisableJsWithJQueryFor($ids, 'on', '!=');
  }

  /**
   * Returns the Javascript to disable or enable a given input or inputs when a checkbox's value changes.
   *
   * @param array $ids Inputs to disable when the condition is false.
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getDisableEnableJsForCheckbox($ids, $use_jquery = false)
  {
    return ($use_jquery === false) ? self::getEnableDisableJsFor($ids, 'on', '==') : self::getEnableDisableJsWithJQueryFor($ids, 'on', '==');
  }

  /**
   * Returns the Javascript to disable or enable a given date input when a checkbox's value changes.
   *
   * @param array $id Inputs to disable when the condition is false.
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getEnableDisableDateJsForCheckbox($id)
  {
    return self::getEnableDisableJsForCheckbox(array($id."_day",$id."_month", $id."_year"));
  }

  /**
   * Returns the Javascript to disable or enable a given input or inputs when the condition for input.value $comparator $value if true.
   *
   * @param array $ids Inputs to disable when the condition is false or to be enabled if condition is true
   * @param String $value
   * @param String comparator
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getEnableDisableJsFor($ids, $value = '0', $comparator = '==')
  {
    $js = "";
    foreach ($ids as $id)
    {
      $js .= "if ($(this).getValue() ${comparator} '${value}') { $('".$id."').disable() } else { $('".$id."').enable() }";
    }
    return $js;
  }

  /**
   * Returns the Javascript to disable or enable a given input or inputs when the condition for input.value $comparator $value if true.
   * This function uses jQuery instead of prototype.
   *
   * @param array $ids Inputs to disable when the condition is false or to be enabled if condition is true
   * @param String $value
   * @param String comparator
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getEnableDisableJsWithJQueryFor($ids, $value = '0', $comparator = '==')
  {
    $js = '';
    foreach ($ids as $id)
    {
      $js .= sprintf(<<<EOF
 var i = jQuery('#%s'); if (jQuery(this).val() %s '%s') { i.attr('disabled', 'disabled'); } else { i.removeAttr('disabled'); };
EOF
        ,
        $id,
        $comparator,
        $value
      );
    }

    return $js;
  }

  /**
   * Returns the Javascript to hide or show a given date input when a checkbox's value changes.
   *
   * @param array $id Inputs to hide when the condition is false.
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getShowHideJsForCheckbox($ids)
  {
    return self::getShowHideJsFor($ids, 'on', '!=');
  }

  /**
   * Returns the Javascript to hide or show a given date input when a checkbox's value changes.
   *
   * @param array $id Inputs to hide when the condition is false.
   *
   * return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getShowHideDateJsForCheckbox($id)
  {
    return self::getShowHideJsForCheckbox(array($id."_day",$id."_month", $id."_year"));
  }

  /**
   * Returns the Javascript to hide or show a given input or inputs when the condition for input.value $comparator $value if true.
   *
   * @param array $ids Inputs to hide when the condition is false or to be shown if condition is true
   * @param String $value
   * @param String comparator
   *
   * @return String Returns the javascript to enable/disable the given inputs.
   */
  public static function getShowHideJsFor($ids, $value = '0', $comparator = '==')
  {
    $js = "";
    foreach ($ids as $id)
    {
      $js .= "if ($(this).getValue() ${comparator} '${value}') { $('".$id."').show() } else { $('".$id."').hide()}";
    }
    return $js;
  }
}
